<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Withdrawal
 * Desc  提现控制器
 * Create on 2024/9/30 11:20
 * Create by wangyafang
 */

namespace app\api\controller\wddj\trailer;


use app\api\model\wddj\trailer\Driver;
use app\common\controller\Api;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Withdrawal extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $driverObj = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\trailer\Withdrawal();
        $wechat_id = \app\api\model\wddj\User::getWechatId($this->auth->id);
        $driverObj = (new \app\api\model\wddj\trailer\Driver())->where('wechat_id',$wechat_id)->find();
        if (!$driverObj) {
            $this->error('不是司机，没有权限,请先认证');
        }
        $this->driverObj = $driverObj;
    }

    /**
     * Desc 立即提现
     * Create on 2024/9/30 11:32
     * Create by wangyafang
     */
    public function submit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $amount = $this->request->post('amount');

        if (empty($amount)) {
            $this->error('提现金额不能为空');
        }

        if ($amount < 0) {
            $this->error('提现金额必须大于0');
        }

        $driverWithdrawalDataQuery = \app\api\model\wddj\trailer\Withdrawal::getWithdrawalData($this->driverObj);
        if($amount > $driverWithdrawalDataQuery['pending_withdrawal']) {
            $this->error('提现金额不能大于待提现余额');
        }

        $bank_id = $this->request->post('bank_id');
        $bankObj = (new \app\api\model\wddj\trailer\Bank())->where('id',$bank_id)->field('name,bank_name,bank_card_number')->find();
        if (!$bankObj) {
            $this->error('银行卡信息不存在');
        }
        $bankArray = $bankObj->toArray();
        $data = array_merge(
            array(
                'wechat_id'=>$this->driverObj->wechat_id,
                'driver_id'=>$this->driverObj->id,
                'amount'=>$amount,
                'bank_id'=>$bank_id,
            ),$bankArray
        );

        $result = false;
        Db::startTrans();
        try {
            $driverObj = (new Driver())->lock(true)->find($this->driverObj->id);
            $driverObj->balance = bcsub($driverObj->balance,$amount,2);
            $driverObj->save();
            $result = $this->model->data($data);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if ($result == false) {
            $this->error('提交失败');
        }

        $this->success('提交成功');
    }
}



 